<?php
/**
 * 用户类
 */
namespace app\admin\controller;

use app\admin\model\Rolemodel;
use think\App;
use think\facade\View;
use app\admin\model\Usermodel;
use app\admin\model\Classroom;
use app\admin\validate\User as validate;

class Classuser extends Common
{
    protected $roleModel = '';
    protected $croommodel;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new Usermodel();
        parent::__construct($app);
        $this->roleModel = new Rolemodel();
        $this->croommodel = new Classroom();
    }

    //教室端用户
    public function index(){
        $where = [];
        $dataArr = $this->request->request('search/a', []);

        if($dataArr['username'])  $where[] = ['username', 'like', "%".$dataArr['username']."%"];
        if($dataArr['mobile'])  $where[] = ['mobile', 'like', "%".$dataArr['mobile']."%"];
        if($dataArr['status'])  $where[] = ['u.status', '=', $dataArr['status']];
        if($dataArr['cid'])  $where[] = ['u.cid', '=', $dataArr['cid']];
        if(session('adminUserInfo.rid') == 7){
            $where[] = [['rid', '=', 4]];
            $roleWhere = [['id', '=', 4]];
            $croomWhereData = [['agent_uid', '=', session('adminUserInfo.uid')]];
        }else{
            if($dataArr['rid'])  $where[] = ['rid', '=', $dataArr['rid']];
            $roleWhere = [['id', 'NOT IN', '1, 2, 5, 6, 7'], ['status', '=', 1], ['is_delete', '=', 1]];
        }

        $roleRes = $this->roleModel->findAllInfo('id, name', $roleWhere, ['id'=>'asc']);
        $croomData = $this->croommodel->findAllInfo('id, name', $croomWhereData);
        if($croomData && session('adminUserInfo.rid') == 7){
            foreach($croomData as $croomVal){
                $ids[] = $croomVal['id'];
            }
            $where[] = ['u.cid', 'in', implode(',', $ids)];
        }

        $userRes = $this->localModel->findAllUserInfo($where, true);

        View::assign(['userData'=>$userRes, 'roleRes'=>$roleRes, 'type'=>2, 'username'=>$dataArr['username'], 'mobile'=>$dataArr['mobile'], 'rid'=>$dataArr['rid'], 'status'=>$dataArr['status'], 'cid'=>$dataArr['cid'], 'croomData'=>$croomData]);
        return view('index');
    }

    //新增页面
    public function add(){
        $type = $this->request->get('type', 1);
        $id = $this->request->get('id', 0);
        $index = $this->request->get('index', 0);
        $cid = $this->request->get('cid', 0);

        if(session('adminUserInfo.rid') == 7){
            $roleWhere = [['id', '=', 4], ['status', '=', 1], ['is_delete', '=', 1]];
            $croomWhereData = [['agent_uid', '=', session('adminUserInfo.uid')]];
        }else{
            $roleWhere = [['id', 'NOT IN', '1, 2, 5, 6, 7'], ['status', '=', 1], ['is_delete', '=', 1]];
        }

        $roleRes = $this->roleModel->findAllInfo('id, name', $roleWhere, ['id'=>'asc']);
        $croomData = $this->croommodel->findAllInfo('id, name', $cid ? [['id', '=', $cid]] : $croomWhereData);

        if($id){
            $userInfo = $this->localModel->findDetaiInfoById('*', $id);
            View::assign(['userInfo'=>$userInfo, 'id'=>$id, 'roleRes'=>$roleRes, 'type'=>$type, 'index'=>$index, 'croomData'=>$croomData]);
            return view('edit');
        }else{
            View::assign(['roleRes'=>$roleRes, 'type'=>$type, 'index'=>$index, 'cid'=>$cid, 'croomData'=>$croomData]);
            return view('add');
        }
    }

    //数据入库
    public function save(){
        $dataArr = $this->request->request('user/a', []);
        $index = $this->request->post('index', 0);
        $id = $this->request->post('id', 0);

        if($dataArr['rid'])  $dataArr['admintype'] = 3;


        if($msg = $this->checkData($dataArr, $id ? ($index ? 'cuedit' : 'cedit') : 'cadd'))  return json(['result'=>'fail', 'title'=>$msg]);

        if($this->localModel->getUserInfoByName($dataArr['username'], $id))  return json(['result'=>'fail', 'title'=>'该用户名已存在！']);

        $res = $this->localModel->saveInfo($dataArr, $id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        if(!$index){
            $url = (string)url('Classuser/index');
        }else{
            $url = (string)url('Index/index');
        }

        $this->logsData = ['cid'=>$id ? $id : $res, 'action'=>$id ? 'edit' : 'add', 'msg'=>$dataArr['username']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>$url]);
    }

}
